package m_dsp_advertiser

import (
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"time"
	"wx-api/dsp-api/common"
	"wx-api/dsp-api/config"
)

type DspAdvertiser struct {
	Id         bson.ObjectId `bson:"_id" json:"id,omitempty"`
	Code       string        `bson:"code" json:"code,omitempty"`               //id
	Name       string        `bson:"name" json:"name,omitempty"`               //名称
	Budget     float64       `bson:"budget" json:"budget,omitempty"`           //剩余预算
	BudgetCum  float64       `bson:"budget_cum" json:"budget_cum,omitempty"`   //累计预算
	Cost       float64       `bson:"cost" json:"cost,omitempty"`               //实时消耗
	TCost      float64       `bson:"tcost" json:"tcost,omitempty"`             //今日消耗
	YCost      float64       `bson:"ycost" json:"ycost,omitempty"`             //昨日消耗
	Flag       int           `bson:"flag" json:"flag,omitempty"`               //1 开启 2 关闭
	Status     int           `bson:"status" json:"status,omitempty"`           //1 正常 2 删除
	CreateTime int64         `bson:"create_time" json:"create_time,omitempty"` //创建时间
	UpdateTime int64         `bson:"update_time" json:"update_time,omitempty"` //更新时间
	Version    string        `bson:"version" json:"version,omitempty"`         //版本号
}

func dbGet() *common.TkMongo {
	return config.MgTableDspAdvertiser
}

func Save(a DspAdvertiser) (string, error) {
	a.Id = bson.NewObjectId()
	tmptime := time.Now().Unix()
	a.CreateTime = tmptime
	a.UpdateTime = tmptime
	a.Flag = 1
	a.Status = 1
	a.Version = config.SysVersion
	err := dbGet().Insert(a)
	return a.Id.Hex(), err
}

// 更新
func Update(a DspAdvertiser) error {
	m := bson.M{
		"name":        a.Name,
		"update_time": time.Now().Unix(),
	}

	return dbGet().Set(bson.M{"_id": a.Id}, m)
}
func Update_budget(a DspAdvertiser) error {
	m := bson.M{
		"budget":      a.Budget,
		"update_time": time.Now().Unix(),
	}

	return dbGet().Set(bson.M{"_id": a.Id}, m)
}

// 更新状态
func UpdateFalg(id string, flag int) error {
	m := bson.M{
		"flag":        flag,
		"update_time": time.Now().Unix(),
	}

	return dbGet().Set(bson.M{"_id": bson.ObjectIdHex(id)}, m)
}

// 删除
func UpdateDel(id string, status int) error {
	m := bson.M{
		"status":      status,
		"update_time": time.Now().Unix(),
	}

	return dbGet().Set(bson.M{"_id": bson.ObjectIdHex(id)}, m)
}

// 获取所有
func GetAll() []DspAdvertiser {
	list := []DspAdvertiser{}
	_ = dbGet().All(nil, &list)
	return list
}

func GetWAll(a int64) []DspAdvertiser {
	list := []DspAdvertiser{}
	_ = dbGet().All(bson.M{"update_time": bson.M{"$gte": a}}, &list)
	return list
}

// 获取详情
func GetByCode(id string) (*DspAdvertiser, error) {
	a := &DspAdvertiser{}
	err := dbGet().FindOne(bson.M{"_id": bson.ObjectIdHex(id)}, a)
	if err != nil {
		if err == mgo.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}
	return a, nil
}
func GetByCk(id string) (*DspAdvertiser, error) {
	a := &DspAdvertiser{}
	w := bson.M{"_id": bson.ObjectIdHex(id)}
	err := dbGet().FindOne(w, a)
	if err != nil {
		if err == mgo.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}
	return a, nil
}

// 列表，带分页
func List(id, name, adx_uid string, flag, status, pageNum, pageSize int) (*common.PageInfo, error) {
	s := bson.M{}
	if id != "" {
		s["_id"] = bson.ObjectIdHex(id)
	}
	if name != "" {
		s["name"] = bson.M{"$regex": bson.RegEx{name, "i"}}
	}
	if adx_uid != "" {
		s["adx_uid"] = adx_uid
	}
	if flag > 0 {
		s["flag"] = flag
	}
	if status > 0 {
		s["status"] = status
	}
	var list []DspAdvertiser
	return dbGet().FindPageInfo(s, "", pageSize, pageNum, "-update_time", &list)
}
